//
//  HSPcommentViewController.m
//  Icar
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPcommentViewController.h"
#import "BaseHeader.h"
#import "AFNBlock.h"
#import "HSPCommentCell.h"
@interface HSPcommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableViewComment;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *commentArrayName;
@property (nonatomic,strong)UIView *titleView1;
@property (nonatomic,strong)UILabel *priceLabel;
@end

@implementation HSPcommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _commentArrayName = [[NSMutableArray alloc]init];
    _titleView1 = [[UIView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 64)];
    [_titleView1 setBackgroundColor:BLUECOLOR];
    
    UIImage *image1 = [UIImage imageNamed:@"5.png"];
    UIImage *img1 = [self scaleToSize:image1 size:CGSizeMake(30 * ADJUSTWIDTH, 30 * ADJUSTHEIGHT)];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(10 * ADJUSTWIDTH, 26 * ADJUSTHEIGHT, 90 * ADJUSTWIDTH, 35 * ADJUSTHEIGHT)];
    [backButton setImage:img1 forState:UIControlStateNormal];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBack) forControlEvents:UIControlEventTouchUpInside];
    [_titleView1 addSubview:backButton];
    [self.view addSubview:_titleView1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    
    
    _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(ADJUSTWIDTH * 157.5, 22 * ADJUSTHEIGHT, 100 * ADJUSTWIDTH, 40 * ADJUSTHEIGHT)];
    _priceLabel.font =[UIFont systemFontOfSize:22. * ADJUSTHEIGHT];
    _priceLabel.text = @"评论";
    _priceLabel.textColor = [UIColor whiteColor];
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    [_titleView1 addSubview:_priceLabel];
    [self creatComment];
    [self AlbumaHeader];
   
    
   
}

- (void)backBack
{
    [self.navigationController popViewControllerAnimated:YES];
  
    _titleView1.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    
}
- (UIImage *)scaleToSize : (UIImage *)img size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
- (void)creatComment
{
    _tableViewComment = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableViewComment.delegate = self;
    _tableViewComment.dataSource = self;
    [self.view addSubview:_tableViewComment];
    
    UILabel *footLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 50 * ADJUSTHEIGHT)];
    [footLabel setText:@"没有更多了"];
    [footLabel setTextAlignment:NSTextAlignmentCenter];
    [footLabel setFont:[UIFont systemFontOfSize:19. * ADJUSTWIDTH]];
    [footLabel setTextColor:[UIColor grayColor]];
    [self.tableViewComment setTableFooterView:footLabel];
   
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPcommentViewController *vc = self;
    [_tableViewComment addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewComment.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPcommentViewController *vcc = self;
    [_tableViewComment addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    NSString *documentString = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *pathString = [documentString stringByAppendingString:@"ImgaeComment.plist"];
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v3/cmt/new_topics/%@?articleId=%@&reverse=0&pageNo=1&pageSize=20",self.acceptStr,self.modelImage.cid] block:^(id back) {
            if (_isUp == YES) {
                [self.commentArrayName removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"comments"]) {
                HSPImageAppreciate *model = [HSPImageAppreciate modelWithDictionary:[dic objectForKey:@"1"]];
                [self.commentArrayName addObject:model];
            }
            [self.tableViewComment reloadData];
            [self.tableViewComment.header endRefreshing];
            [self.tableViewComment.footer endRefreshing];
                    [NSKeyedArchiver archiveRootObject:_commentArrayName toFile:pathString];
        }];
        
    }
    if (ISCONENNCTED == NO) {
        if (_isUp == YES) {
            [_commentArrayName removeAllObjects];
        }
        //读取
        self.commentArrayName = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:pathString]];
        [_tableViewComment reloadData];
        [_tableViewComment.header endRefreshing];
        [_tableViewComment.footer endRefreshing];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _commentArrayName.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * ADJUSTHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPCommentCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[HSPCommentCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    HSPImageAppreciate *model = [self.commentArrayName objectAtIndex:indexPath.row];
    self.tableViewComment.scrollsToTop = YES;
    [cell getFloorMessage:model];
  
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
